from flask import render_template
from app import create_app
from flask import request
from flask import jsonify
from app.model_train_predict.lstm_model_train import LSTMModelTrainPredict
# 导入Neo4jGraph
from app.spectrum.map_display import Neo4jGraph

app = create_app()


@app.route('/')
def index():  
    return render_template("index.html")

@app.route('/graph')
def neo4j_graph():
    # 展示图谱页面
    return render_template("index4.html")



# 命名实体识别
@app.route("/lstm_crf_predict",methods=["POST"])
def lstm_crf_predict():
    # 实例化模型对象
    lstm_model_train_predict = LSTMModelTrainPredict()
    # 获取前端界面传递过来的参数
    text = request.get_json()
    # 模型预测
    result = lstm_model_train_predict.model_application("predict",text["text"])
    # 提取出键名和键值
    first_key = list(result.keys())[0] if result else "暂无"
    first_value = list(result.values())[0] if result else "暂无"
    # 将以上两个值进行组装
    result = {
        "text":first_key,
        "type":first_value
    }
    return jsonify({"success":200,"result":result,"text":text["text"]})

@app.route("/neo4j")
def neo4j():
    neo4j_graph = Neo4jGraph()
    neo4j_graph.connect_neo4j()
    print("开始上传数据到 Neo4j...")
    neo4j_graph.load_file_create_map()  # 👈 关键调用
    print("数据上传完成！")
    return "数据已上传至 Neo4j！"




if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)